﻿import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import Hearder from './hearder'
import './details.scss'
import './coupon.scss'

export default class Index extends Component {
  constructor(props) {
    super(props)
    this.state = {
     isload:0,
     couponid:"",
     mid:"",
     byfrom:"",
     info:{publish:{couponType:"",name:"",summary:""}}
    }
  }
  config = {
    navigationBarTitleText:'优惠券详情'
  }
  open(url,type){
    CommonJs.open(url)
  }
  componentWillMount () {
    var couponid=this.$router.preload.couponid;
    var mid=this.$router.preload.mid;
    var byfrom=this.$router.preload.byfrom;
    this.getdate(mid,couponid,byfrom)
    //this.getdate("","656906599985782784")
  }
  componentDidShow () {
  }
  getdate(mid,id,byfrom){
  	var _this=this
  	if(byfrom=="my"){
  		var url="/crm/papi/v1.9/coupon/"+mid+"/"+id;
  	}
  	else{
  		var url="/crm/papi/v1.9/coupon/getAndCheckPublish/"+id;
  	}
  	CommonJs.ajaxRequest(url,'','GET',function(res){
  		if(byfrom=="my"){
  			var ret=res.data
  		}
  		else{
  			var obj={
  				publish:res.data,
  				beginAt:res.data.beginAt,
  				expireAt:res.data.expireAt,
  				mallId:res.data.mall.id,
  				shopVMList:res.data.shopVMList
  			}
  			var ret=obj
  		}
			_this.setState({
	      info:ret,
	      id:id,
	      byfrom:byfrom,
	      isload:1
	    })
  	},"wfw")
  }
  getcoupon(id,mid){
  	var _this=this
  	var url="/crm/papi/v1.9/coupon/receiveCoupon";
  	var dates={
	      id:id,
	      mallId:mid
  	}
  	CommonJs.ajaxRequest(url,dates,'PUT',function(res){
			Taro.showToast({
			  title:"领券成功，请到个人中心查看优惠券",
			  icon: 'none',
			  duration:4000
			})
  	},"wfw")
  }
  zz(){
  	Taro.showToast({
		  title:"暂未开通",
		  icon: 'none',
		  duration: 2000
		})
  }
  getdesc(){
  	var info=this.state.info;
  	var type=info.publish.couponType;
  	if(type.indexOf('LIMIT_CATEGORYID')>=0){
  		return this.dodesc("指定商品分类");
  	}
  	else if(type.indexOf('LIMIT_GOODID')>=0){
  		return this.dodesc("指定商品");
  	}
  	else{
  		return this.dodesc("");
  	}
  }
  dodesc(str){
  	var info=this.state.info;
  	var type=info.publish.couponType;
  	if(type.indexOf('QUOTA')==0){
			return str+info.publish.ruleFavourData/100+"元定额优惠券"
		}
		else if(type.indexOf('CONSUPTION_QUOTA')==0){
			return str+"满"+info.publish.ruleLimitData/100+"元减"+info.publish.ruleFavourData/100+"元优惠券"
		}
		else if(type.indexOf('CONSUPTION_DISCOUNT')==0){
			return str+"满"+info.publish.ruleLimitData/100+"元减"+info.publish.ruleFavourData/10+"折优惠券"
		}
		else if(type.indexOf('COUNT_DISCOUNT')==0){
			return str+"满"+info.publish.ruleLimitData+"件减"+info.publish.ruleFavourData/10+"折优惠券"
		}
		else{
			return str+"每满"+info.publish.ruleLimitData/100+"元减"+info.publish.ruleFavourData/100+"元优惠券"
		}
  }
  render () {
    const red = this.state.info
    return (
      <View className='page' style={this.state.isload==1?'display:block':'display:none'}>
       	<View className='yhq-box'>
       		<View className='yhq-pri' style={red.publish.couponType.indexOf('DISCOUNT')<0?'':'display:none'}>¥ <Text className="pri">{red.publish.ruleFavourData/100}</Text></View>
       		<View className='yhq-pri' style={red.publish.couponType.indexOf('DISCOUNT')>=0?'':'display:none'}><Text className="pri">{red.publish.ruleFavourData/10}</Text> 折</View>
       		<View className='yhq-name'>{red.publish.name}</View>
       		<View className='yhq-btn' style={this.state.byfrom=='my'?'':'display:none'}>
       				<Text className="lj" onClick={this.open.bind(this,'/pages/mybill/mybill')}>立即使用</Text>
       				<Text className="zz" onClick={this.zz.bind(this)}>转赠朋友</Text>
       		</View>
       		<View className='yhq-btn' style={this.state.byfrom=='my'?'display:none':''}>
       				<Text className="lj" onClick={this.getcoupon.bind(this,red.publish.id,red.mallId)}>立即领取</Text>
       		</View>
       		<View className='yhq-time'>有效期:{CommonJs.FormatDate(red.beginAt,2)}到{CommonJs.FormatDate(red.expireAt,2)}</View>
       		<View className='yhq-tj'>
       			{this.getdesc()}
       		</View>
       	</View>
       	<View className='yhq-box' style="margin-top:10px;">
       		<View className='yhq-tit'>适用门店</View>
       		<Text className='yhq-xz'>
       			 {red.shopVMList.map((item, index) => (
       			 		<Text style="padding:10px 20px 0 0">{item.name}</Text>
       			 ))}
       			 
       		</Text>
       	</View>
       	<View className='yhq-box' style="margin-top:10px;">
       		<View className='yhq-tit'>优惠券须知</View>
       		<Text className='yhq-xz'>
       			 {red.publish.summary}
       		</Text>
       	</View>
      </View>
    )
  }
}